package com.example.service;

import com.example.entity.Devices;
import com.example.entity.OrangeLed;
import com.example.entity.Params;
import com.example.repository.OrangeLedMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class OrangeLedService implements DeviceScheduleThreshold, DeviceValueUpdate {

    public static List<Boolean> STATUS_ORANGE_LED_LIST;

    static {
        STATUS_ORANGE_LED_LIST = Lists.newArrayList(Boolean.FALSE);
    }

    @Resource
    private OrangeLedMapper orangeLedMapper;
    @Resource
    private CoSensorService coSensorService;


    public PageInfo<OrangeLed> findOLed(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<OrangeLed> search = orangeLedMapper.findOLed(params);
        return PageInfo.of(search);
    }

    public void addOLed(OrangeLed orangeLed) {
        orangeLedMapper.insert(orangeLed);
    }

    public void deleteOLed(String Oid) {
        orangeLedMapper.deleteById(Oid);
    }

    @Override
    public boolean threshold(Devices devices) {
        return Objects.nonNull(devices.getThreshold())
                && coSensorService.getCO() > devices.getThreshold();
    }

    @Override
    public void statusUpdate(String status) {
        if (StringUtils.equals(status, "1")) {
            STATUS_ORANGE_LED_LIST.set(0, Boolean.TRUE);
        } else {
            STATUS_ORANGE_LED_LIST.set(0, Boolean.FALSE);
        }
    }
}
